<template>
  <div>
    <el-card class="box-card">
      <div slot="header" style="text-align:left">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="关键词">
            <el-input v-model="searchForm.key" placeholder="手机号,邮箱,用户名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="users" border style="width: 100%">
       
        <el-table-column prop="username" label="用户名" style="width: 20%"></el-table-column>
        <el-table-column prop="phone" label="手机" style="width: 50%"></el-table-column>
         <el-table-column prop="email" label="邮箱" style="width: 50%"></el-table-column>
       
        <el-table-column prop="type"  label="账号类型" style="width: 50%">
          <template slot-scope="scope">
            <span style="color:red" v-if="scope.row.type=='admin'">超管</span>
            <span style="color:green" v-else-if="scope.row.type=='merchant'">商户</span>
            <span style="color:gray" v-else>其他</span>
        </template>
        </el-table-column>
        <el-table-column prop="status"  label="状态(锁定)" style="width: 50%">
          <template slot-scope="scope">
              <el-switch @change="changeStatus(scope.row)"
                    v-model="scope.row.status"
                    active-color="#13ce66"
                    inactive-color="#ff4949">
              </el-switch>
            
        </template>
        </el-table-column>
        <el-table-column label="操作" style="width: 10%">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="open(scope.row)">改密</el-button>
            <el-button type="text" size="small" @click="showbind(scope.row)">角色</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top:10px;">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page.sync="pagenow"
          :page-size="pagesize"
          layout="total, prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
     <el-dialog :visible.sync="dialog"  width="50%" center style="text-align: center" >
          <el-transfer :titles="['所有角色', '选中角色']" v-model="chooseRoles" :data="transData"  @change="handleChange" ></el-transfer>
    </el-dialog>
  </div>
</template>

<script>
import { sysuserList,sysuserChangePassword,sysuserChangeStatus,roleNotSystemed,sysuserBind,sysuserHasRole} from "@/libs/api.js";
export default {
  methods: {
    showbind(row){
      this.bindUserId=row.id;
      this.dialog=true;
       sysuserHasRole(row.id).then(res => {
         if(res && res.length>0){
           for(var i=0;i<res.length;i++){
              this.chooseRoles.push(res[i].id)
           }
         }else{
           this.chooseRoles=[];
         }
       });
    },
    
    /**选中商品变化 */
    handleChange(value, direction, movedKeys) {
         var param ={userId:this.bindUserId};
         param.roleIds=value;
         sysuserBind(param).then(res => {
            this.$util.success();
        });
    },
    handleCurrentChange(p) {
      this.loadPage(p);
    },
    onSubmit() {
      this.pagenow = 1;
      this.loadPage();
    },
    loadPage() {
      var param = { pagesize: 10, pagenow: this.pagenow,"key":this.searchForm.key};
      sysuserList(param).then(res => {
        this.users = res.rows;
        this.total = res.total;
      });
    },
    changeStatus(row){
      var param = {id:row.id,status:!row.status}
      sysuserChangeStatus(param).then(res => {
           this.$util.success();
      });
    },
    open(row) {
        this.$prompt('请输入新密码', '', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          var param = {id:row.id,newp:value}
          sysuserChangePassword(param).then(res => {
             this.$util.success();
          });
        });
      }
  },
  role(row){
    
  },
  mounted() {
    this.loadPage();
      roleNotSystemed().then(res => {
      if(res && res.length>0){
          this.transData=[];
          for(var i=0;i<res.length;i++){
            res[i].key=res[i].id;
            res[i].label=res[i].name;
            this.transData.push(res[i]);
          }
      }
    });
  },

  data() {
    return {
      users: [],
      total: 0,
      pagenow: 1,
      pagesize: 10,
      transData:[],
      chooseRoles:[],
      bindUserId:"",//需要绑定角色的userid
      dialog:false,
      searchForm: {
        key: "",
      }
    };
  }
};
</script>